package com.ds.home.database;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ds.home.EIDevice;
import com.ds.home.EIDeviceEndPoint;
import com.ds.home.EISensortype;
import com.ds.home.EIZNode;
import com.ds.home.database.vo.DeviceEndPointVO;
import com.ds.iot.enums.DeviceDataTypeKey;

import java.util.*;

/**
 * DeviceModule entity.
 *
 * @author MyEclipse Persistence Tools
 */

public class DBDeviceEndPoint implements EIDeviceEndPoint {

    private DeviceEndPointVO vo;

    /**
     * minimal constructor
     */
    protected DBDeviceEndPoint(DeviceEndPointVO vo) {
        this.vo = vo;
    }


    public EIDevice getDevice() {

        return MEMManagerImpl.getInstance().getDeviceById(vo.getDeviceId());
    }

    @Override
    public String getDeviceId() {
        return vo.getDeviceId();
    }

    public String getEndPointId() {
        return vo.getEndPointId();
    }

    public void setEndPointId(String endPointId) {
        vo.setEndPointId(endPointId);

    }

    public String getEp() {
        return vo.getEp();
    }

    public void setEp(String ep) {
        vo.setEp(ep);
    }

    public String getHadeviceid() {
        return vo.getHadeviceid();
    }

    public void setHadeviceid(String hadeviceid) {
        vo.setHadeviceid(hadeviceid);
    }

    public String getIeeeaddress() {
        return vo.getIeeeaddress();
    }

    public void setIeeeaddress(String ieeeaddress) {
        this.vo.setIeeeaddress(ieeeaddress);
    }

    public String getName() {
        return vo.getName();
    }

    public void setName(String name) {
        this.vo.setName(name);
    }

    public String getNwkAddress() {
        return vo.getNwkAddress();
    }

    public void setNwkAddress(String nwkAddress) {
        this.vo.setNwkAddress(nwkAddress);
    }

    public String getProfileid() {
        return vo.getProfileid();
    }

    public void setProfileid(String profileid) {
        this.vo.setProfileid(profileid);
    }

    public void setCurrvalue(String name, String currvalue) {
        Map<DeviceDataTypeKey, String> valueMap = getCurrvalue();

        if (currvalue == null) {
            valueMap.remove(name);

        } else {
            valueMap.put(DeviceDataTypeKey.fromType(name), currvalue);

        }
        valueMap.put(DeviceDataTypeKey.LastUpdateTime, Long.toString(System.currentTimeMillis()));
        String json = JSONObject.toJSON(valueMap).toString();
        MEMManagerImpl.getInstance().getCurrValueCache().put(this.getEndPointId(), json);
        this.vo.setValue(json);
    }

    public synchronized   Map<DeviceDataTypeKey, String> getCurrvalue() {
        String json = this.vo.getValue();
        if (MEMManagerImpl.getInstance().getCurrValueCache().get(this.getEndPointId()) != null) {
            json = MEMManagerImpl.getInstance().getCurrValueCache().get(this.getEndPointId());
        }

        Map<DeviceDataTypeKey, String> valuemap = new HashMap<DeviceDataTypeKey, String>();

        if ((json != null) && (!json.equals("") && json.startsWith("{") && json.endsWith("}"))) {
            JSONObject obj = JSONObject.parseObject(json);
            Set<String> keys = obj.keySet();
            for (String key : keys) {
                if (key != null) {
                    valuemap.put( DeviceDataTypeKey.fromType(key), obj.getString(key));
                }

            }
        }

        return valuemap;
    }

    public EIZNode addZNode(EIZNode znode) {
        Set znodeIds = this.getAllZNodeIds();
        if (!znodeIds.contains(znode.getZnodeid())) {
            znodeIds.add(znode.getZnodeid());

            MEMManagerImpl.getInstance().getAllZNodeIdCache().put(this.getEndPointId(), JSONObject.toJSONString(znodeIds));
        }
        return znode;
    }

    public void removeZNode(String znodeId) {
        Set znodeIds = this.getAllZNodeIds();
        if (znodeIds.contains(znodeId)) {
            znodeIds.remove(znodeId);
            MEMManagerImpl.getInstance().getAllZNodeIdCache().put(this.getEndPointId(), JSONObject.toJSONString(znodeIds));
        }

    }

    public Set<String> getAllZNodeIds() {
        String idsjson = MEMManagerImpl.getInstance().getAllZNodeIdCache().get(this.getEndPointId());
        Set znodeIds = new LinkedHashSet();
        if (idsjson == null) {
            List<EIZNode> znodeList = MEMManagerImpl.getInstance().getZNodesByEndPointId(this.getEndPointId());
            for (EIZNode znonde : znodeList) {
                znodeIds.add(znonde.getZnodeid());
            }
            idsjson = JSONObject.toJSONString(znodeIds);
            MEMManagerImpl.getInstance().getAllZNodeIdCache().put(this.getEndPointId(), JSONObject.toJSONString(znodeIds));
        }
        znodeIds = new LinkedHashSet(JSONArray.parseArray(idsjson, String.class));
        return znodeIds;
    }

    public List<EIZNode> getAllZNodes() {
        List<EIZNode> znodes = new ArrayList<EIZNode>();
        Set<String> znodeIds = getAllZNodeIds();
        for (String znodeid : znodeIds) {
            EIZNode znode = MEMManagerImpl.getInstance().getZNodeById(znodeid);
            if (znode != null) {
                znodes.add(znode);
            }

        }
        return znodes;
    }

    public EISensortype getSensortype() {
        EISensortype sensortype = MEMManagerImpl.getInstance().getSensoTypeByNo(vo.getSensorType());

        return sensortype;

    }

    public void setSensortype(Integer sensorType) {
        vo.setSensorType(sensorType);
    }

    public DeviceEndPointVO getVo() {
        return vo;
    }

    public void setVo(DeviceEndPointVO vo) {
        this.vo = vo;
    }

}